package org.itboys.mobile.service.mongo.weixin;


import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mongo.ZhongBaoContact;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：众包_微信_通信录_service
 */
@Service
public class ZhongBaoWeiXinContactService extends BaseMongoService<ZhongBaoContact> {

    @Autowired
    private SundayWeiXinMemberService memberService;

    @Resource(name = "mobileDs")
    private MongoDataSource mobileDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mobileDataSource;
    }

    @Override
    protected Class<ZhongBaoContact> getEntityClass() {
        return ZhongBaoContact.class;
    }


    /**
     * 获取通讯录
     * @param request
     * @param memberId
     * @param type 1，员工，2同事，3客户
     * @return
     */
    public List<ZhongBaoContact> getContact(HttpServletRequest request, Long memberId, Integer type){
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("isDeleted",0);

        //1，获取员工和同事
        if(type==1||type==2){
            SundayMember self = memberService.getById(memberId);
            //自己是否是管理员
            boolean isManager= LongUtil.isZreo(self.getParentId())?true:false;
            logger.info("isManager="+isManager);
            if(isManager){
                param.put("parentId",memberId);
            }else{
                param.put("parentId",self.getParentId());
            }
            param.put("type in",new Integer[]{1,2});
            //2,获取客户。只获取自己的客户
        }else{
            param.put("parentId",memberId);
            param.put("type",3);
        }



        //是否包含管理员，查询员工的时候不包含管理员
        return super.pageQuery(request,param).getData();
    }
    /**
     * 新增或修改通讯录
     * @param contactId
     * @param parentId
     * @param type 1，员工，2同事，3客户
     * @param nickName
     * @param mobile
     * @param remark
     */
    public void saveContact(Long contactId,Long parentId,String nickName,String mobile,String remark,Integer type){
        //修改
        if(LongUtil.isNotZreo(contactId)){
            if(type==1||type==2){
                //校验手机号
                Map<String,Object> param = new HashMap<String,Object>();
                param.put("isDeleted", SundayCommonConstants.NO);
                param.put("id !=",contactId);
                param.put("mobile",mobile);
                if(super.count(param)>0l){
                    throw new ErrorException("该手机号已被使用");
                }
            }
            //ZhongBaoEmployee employee=;
            super.updateExceptEmpty(contactId,new ZhongBaoContact(parentId,nickName,mobile,remark,type));
        }else{
            if(type==1||type==2){
                Map<String,Object> param = new HashMap<String,Object>();
                param.put("isDeleted", SundayCommonConstants.NO);
                param.put("mobile",mobile);
                if(super.count(param)>0l){
                    throw new ErrorException("该手机号已被使用");
                }
            }

            super.save(new ZhongBaoContact(parentId,nickName,mobile,remark,type));
        }
        //return super.pageQuery(request,param).getData();
    }
}
